package cn.edu.neusoft.food729.model;

import java.util.List;

import cn.edu.neusoft.food729.Bean.FoodListBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.FoodListiface;
import cn.edu.neusoft.food729.listener.FoodListListener;
import cn.edu.neusoft.food729.service.FoodListService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodListModel implements FoodListiface {
    private Retrofit retrofit;

    public FoodListModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    @Override
    public void getResultList(int shop_id, final FoodListListener listener) {
        FoodListService service = retrofit.create(FoodListService.class);
        Call<List<FoodListBean>> call = service.getFoodByShop(shop_id);

        call.enqueue(new Callback<List<FoodListBean>>() {
            @Override
            public void onResponse(Call<List<FoodListBean>> call, Response<List<FoodListBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
                else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<FoodListBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--DemoModel--" + t.toString());
            }
        });
    }

    @Override
    public void getSearchList(String search, final FoodListListener listener) {
        FoodListService service = retrofit.create(FoodListService.class);
        Call<List<FoodListBean>> call = service.getFoodBySearch(search);

        call.enqueue(new Callback<List<FoodListBean>>() {
            @Override
            public void onResponse(Call<List<FoodListBean>> call, Response<List<FoodListBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body());
                else listener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<FoodListBean>> call, Throwable t) {
                listener.onFail(t.toString());
                System.out.println("--DemoModel--" + t.toString());
            }
        });

    }

}

